package com.media.themovieworld.util.network;

import java.net.SocketTimeoutException;
import java.util.concurrent.TimeoutException;

import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;

import com.media.themovieworld.MovieWorldApp;
import com.media.themovieworld.R;
import com.media.themovieworld.util.network.util.NetworkUtility;


public abstract class HttpMovieTask<Params,Result> extends AsyncTask<Params, Void, Result>{

	// ===================================================================
	// Constants
	// ===================================================================
	private static final String LOG_TAG = HttpMovieTask.class.getSimpleName();

	// ===================================================================
	// Fields
	// ===================================================================
	protected View loadingView ;
	protected ViewGroup  mViewParent;
	private boolean mInitLoadingView = false;
	protected Context mContext;
	protected boolean isShowProgress = true;
	// ===================================================================
	// Constructors
	// ===================================================================

	// ===================================================================
	// Getter & Setter
	// ===================================================================

	// ===================================================================
	// Methods from BaseClass & Interfaces
	// ===================================================================

	// ===================================================================
	// Methods
	// ===================================================================
	public void run(Context pContext,ViewGroup pViewParent,Params... params){
		this.mViewParent = pViewParent;
		this.mContext = pContext;
//		if(this.checkNetwork()){
		this.running(params);
//		} else {
////			//Toast.makeText((Context)pBaseActivity,"No Network",//Toast.LENGTH_LONG).show();
//		}
	}
	
	public void run(Context pContext,ViewGroup pViewParent,boolean isShowProgress,Params... params){
		this.mViewParent = pViewParent;
		if(!isShowProgress) mViewParent = null;
		this.mContext = pContext;
//		if(this.checkNetwork()){
		this.running(params);
//		} else {
////			//Toast.makeText((Context)pBaseActivity,"No Network",//Toast.LENGTH_LONG).show();
//		}
	}
	
	private void running(Params... params){
		if(this.loadingView == null){
		this.loadingView = LayoutInflater.from(mContext).inflate(R.layout.layout_loading, null);
		}
		if(this.mViewParent != null){
				if(this.mInitLoadingView == false){
				this.mViewParent.addView(this.loadingView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
				this.mInitLoadingView = true;
				} else {
					this.loadingView.setVisibility(View.VISIBLE);
				}
		}
		if (checkNetwork()) {
			this.execute(params);
		}else{
			onPostExecute(null);
		}
	}
	
	private void removeLoadingView(){
		if(this.mViewParent!= null && this.loadingView !=null && this.mInitLoadingView ){
			this.loadingView.setVisibility(View.GONE);
			this.mViewParent.removeView(this.loadingView);
			this.mInitLoadingView = false;
		}
	}
	
	@Override
	protected final Result doInBackground(Params... params) {
		
			try {
				return this.processInBackground(params);
			} catch (SocketTimeoutException e) {
				
				e.printStackTrace();
//				this.removeLoadingView();
//				this.onDestroy();
				return null;
			} catch (TimeoutException e) {
				
				e.printStackTrace();
//				this.removeLoadingView();
//				this.onDestroy();
				return null;
			}
		
	}
	@Override
	protected final void onPostExecute(Result result) {
		
		super.onPostExecute(result);
		this.onUIUpDate(result);
		this.removeLoadingView();
		this.onDestroy();
	}
	@Override
	protected final void onPreExecute() {
		
		super.onPreExecute();
	}
	@Override
	protected void onCancelled() {
		onTaskCancel();
		removeLoadingView();
		super.onCancelled();
	}
	
	public void onDestroy(){
		this.loadingView = null;
	}
	
	protected boolean checkNetwork(){
		if(this.mContext != null){
		return NetworkUtility.isInternetConnected(mContext);
		}
		return false;
	}
	// ===================================================================
	// Methods for SubClass
	// ===================================================================
	public abstract Result processInBackground(Params... pParams) throws SocketTimeoutException,TimeoutException;

	public abstract void onUIUpDate(Result result);
	
	public void onTaskCancel() {
	}

	// ===================================================================
	// Inner and Anonymous Classes
	// ===================================================================
}


